<template>
	<view class="cart-container">
		<view class="cart-header">
			<checkbox-group @change="checkAllChange">
				<label>
					<checkbox value="all" :checked="isAllChecked" color="#ff0000" />
					全选
				</label>
			</checkbox-group>
		</view>
		<view class="cart-list">
			<block v-for="(item, index) in cartList" :key="index">
				<view class="cart-item">
					<checkbox-group @change="checkChange">
						<label>
							<checkbox :value="item.id" :checked="item.checked" color="#ff0000" />
						</label>
					</checkbox-group>
					<image :src="item.imgSrc" mode=""></image>
					<view class="item-info">
						<text>{{ item.name }}</text>
						<text>￥{{ item.price }}</text>
					</view>
					<view class="item-control">
						<button @click="decreaseCount(item)">-</button>
						<input type="number" v-model="item.count" disabled />
						<button @click="increaseCount(item)">+</button>
					</view>
				</view>
			</block>
		</view>
		<view class="cart-footer">
			总价：￥{{ totalPrice }}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cartList: [{
						id: 1,
						name: '商品A',
						price: 99.99,
						count: 1,
						imgSrc: 'https://example.com/imageA.jpg',
						checked: true
					},
					{
						id: 2,
						name: '商品B',
						price: 59.99,
						count: 2,
						imgSrc: 'https://example.com/imageB.jpg',
						checked: false
					},
					{
						id: 3,
						name: '商品C',
						price: 79.99,
						count: 1,
						imgSrc: 'https://example.com/imageC.jpg',
						checked: true
					}
				],
				isAllChecked: false,
			};
		},
		created() {
			// this.fetchCartData();
		},
		computed: {
			totalPrice() {
				let total = 0;
				this.cartList.forEach(item => {
					if (item.checked) {
						total += item.price * item.count;
					}
				});
				return total;
			}
		},
		methods: {
			async fetchCartData() {
				try {
					const response = await uni.request({
						url: '/api/cart',
						method: 'GET'
					});
					this.cartList = response.data;
					this.updateAllCheckedStatus();
				} catch (error) {
					console.error('获取购物车数据失败:', error);
				}
			},
			updateAllCheckedStatus() {
				this.isAllChecked = this.cartList.every(item => item.checked);
			},
			checkAllChange(e) {
				this.isAllChecked = e.detail.value.includes('all');
				this.cartList.forEach(item => {
					item.checked = this.isAllChecked;
				});
			},
			checkChange(e) {
				const checkedItems = e.detail.value;
				this.cartList.forEach(item => {
					item.checked = checkedItems.includes(item.id);
				});
				this.isAllChecked = this.cartList.every(item => item.checked);
			},
			increaseCount(item) {
				item.count++;
			},
			decreaseCount(item) {
				if (item.count > 1) {
					item.count--;
				}
			}
		}
	};
</script>

<style scoped>
	/* 页面样式 */
	.cart-container {
		display: flex;
		flex-direction: column;
	}

	.cart-header {
		padding: 10px;
	}

	.cart-list {
		flex: 1;
		overflow-y: auto;
	}

	.cart-item {
		display: flex;
		align-items: center;
		padding: 10px;
	}

	.item-info {
		flex: 1;
		margin-left: 10px;
	}

	.item-control {
		display: flex;
		align-items: center;
	}

	.cart-footer {
		padding: 10px;
		text-align: right;
	}
</style>